package com.ssyc.gsk_master.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.IndicatorManager;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.view.vp.SuperViewPager;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.fragment.MasterDiyRedPackageFragment;
import com.ssyc.gsk_master.fragment.MasterRedPackageFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * 	新版发红包（校长端）
 */
public class MasterCreatRedPackageActivity extends FragmentActivity {

	// 返回
	LinearLayout llBack;

	// 标题
	TextView tvTitle;

	//皮肤
	TextView tv_pifu;

	// 导航条
	MagicIndicator indicator;

	// VP
	SuperViewPager superVp;


	// Tab的标题集合
	private String[] mTites = { "拼手气红包", "定制红包" };

	// 教室classnum
	private String classNum;

	// ViewPager的Framgent集合
	private List<Fragment> mFramgents;
	private VpAdapter adapter;
	private Intent intent;
	private String account;
	private String user_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.master_activity_creatredpackage);

//		user_id = getIntent().getStringExtra("user_id");

		account = AccountUtils.getAccount(this);

		// 设置6.0以后沉浸状体栏的问题
		setBar();

		initView();

		initListener();

		getIntentData();

		initMag();

		initData();

	}

	/**
	 * 初始化View方法
	 */
	private void initView() {
		llBack =  findViewById(R.id.ll_back);
		indicator =  findViewById(R.id.mag);
		superVp = findViewById(R.id.vp);
		tv_pifu = findViewById(R.id.tv_pifu);
	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub

		SPUtil.put(this, "imagePath", "");
    	SPUtil.put(this, "themeId", "");
    	SPUtil.put(this, "diyJson", "");
		super.onDestroy();
	}


	/**
	 * 设置监听的方法
	 */
	private void initListener() {

		tv_pifu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MasterCreatRedPackageActivity.this,
						MasterRedPacageTemplateActivity.class);
				startActivity(intent);
			}
		});

		llBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
	}

	/**
	 * 获取页面传递过来的值
	 */
	public void getIntentData() {



	}

	/**
	 * 初始化导航条的方法
	 */
	private void initMag() {
		IndicatorManager.setIndicator(this, 0, indicator, superVp, mTites,
				getResources().getColor(R.color.white), getResources()
						.getColor(R.color.white), null);
	}

	/**
	 * 初始化页面数据,这列需要请求服务器来获取数据
	 */
	private void initData() {
		mFramgents = new ArrayList<Fragment>();

		MasterRedPackageFragment fragment1 = new MasterRedPackageFragment();
		MasterDiyRedPackageFragment fragment2 = new MasterDiyRedPackageFragment();

		mFramgents.add(fragment1);
		mFramgents.add(fragment2);

		// 初始化ViewPager控件
		initVp();
	}


	/**
	 * 设置ViewPager
	 */
	private void initVp() {
		adapter = new VpAdapter(getSupportFragmentManager());
		superVp.setAdapter(adapter);
		superVp.setOffscreenPageLimit(mFramgents.size());
	}

	/**
	 * ViewPager的适配器
	 */
	public class VpAdapter extends FragmentStatePagerAdapter {

		public VpAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return mFramgents.get(position);
		}

		@Override
		public int getCount() {
			return mFramgents.size();
		}
	}

	/**
	 * 设置沉浸状态栏的方法,需要在setContentView()之后调用
	 */
	private void setBar() {
		// 兼容5.0+，状态栏设置透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = getWindow();
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
		} else
		// 为了沉浸式状态栏 4.4以上才有
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
	}

}
